package guoanmaker.operator.business.controller;

import guoanmaker.operator.business.model.InRoundPhone;
import guoanmaker.operator.business.model.OperatorWelcomes;
import guoanmaker.operator.business.service.OperatorWelcomesServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**欢迎语
 * @author huZhaoyu 
 * @ClassName: ${type_name} 
 * @Description: ${todo} 
 * @date 2017/6/27 ${time} 
 * ${tags} 
 */
@RestController
@RequestMapping(ApiController.OPERATOR_WELCOMES_URL)
public class OperatorWelcomesController {
    private final AjaxJson ajaxJson;
    private final OperatorWelcomesServiceInterface operatorWelcomesService;

    @Autowired
    public OperatorWelcomesController(AjaxJson ajaxJson, OperatorWelcomesServiceInterface operatorWelcomesService) {
        this.ajaxJson = ajaxJson;
        this.operatorWelcomesService = operatorWelcomesService;
    }

    /**
     * @return AjaxJson   
     * @Title: saveWelcomes 
     * @Description: 保存首页欢迎语 
     * @param: OperatorWelcomes
     * @throws 
     */
    @ApiOperation(value = "保存首页欢迎语", notes = "保存首页欢迎语，包括新增与修改 ")
    @PostMapping(value = "saveWelcomes")
    public AjaxJson saveWelcomes(@RequestBody InRoundPhone inRoundPhone) {
    	
    	try{
    		OperatorWelcomes ow=inRoundPhone.getOperatorWelcomes();
    		ow.setCreateDate(new Date());
    		OperatorWelcomes    operatorWelcomes = operatorWelcomesService.saveWelcomes(ow);
        ajaxJson.setSuccess(true).setStatus(200).setData(operatorWelcomes);
    	}catch(Exception e){
    		e.printStackTrace();
    		 ajaxJson.setSuccess(true).setStatus(250).setData(null);
    	}
        return ajaxJson;
    }

    /** 
    * @Title: getAllWelcomes
    * @Description: 查询全部欢迎语 
    * @param    
    * @return AjaxJson    
    * @throws 
    */
    @ApiOperation(value = "查询所有欢迎语", notes = "查询所有欢迎语")
    @PostMapping(value = "getAllWelcomes")
    public AjaxJson getAllWelcomes() {
    	try{
    	List<OperatorWelcomes> ops=operatorWelcomesService.getAllWelcomes();
    	if(ops.size()>0){
        ajaxJson.setSuccess(true).setStatus(200).setData(ops.get(0));
        }else{
        	ajaxJson.setSuccess(true).setStatus(200).setData(null);
        }
    	}catch(Exception e){
    		e.printStackTrace();
    		 ajaxJson.setSuccess(false).setStatus(250).setData(null);
    	}
    	return ajaxJson;
    }

 
    /** 
     * @Title: getAllWelcomes
     * @Description: 查询全部欢迎语 
     * @param    
     * @return AjaxJson    
     * @throws 
     */
    @ApiOperation(value = "删除欢迎语", notes = "删除首页欢迎语")
    @DeleteMapping(value = "delete/{id}")
    public AjaxJson deleteWelcomes(@PathVariable(value = "id")String id) {
        operatorWelcomesService.deleteWelcomesById(id);
        return ajaxJson.setSuccess(true).setStatus(200).setData("数据删除成功!");
    }
    
    
    @ApiOperation(value = "查找欢迎语", notes = "根据Id查找欢迎语")
    @GetMapping(value = "getWelcomesByCityCode")
    public AjaxJson getWelcomesById(@RequestParam(value = "id") String id) {
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorWelcomesService.getWelcomesById(id));
    }
}
